package com.wulee.notebook.bao;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageButton;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;

import com.just.agentweb.WebChromeClient;
import com.just.agentweb.WebViewClient;
import com.wulee.notebook.R;

/**
 * Created by lenovo on 2018/1/6.
 */
public class NoticeDetailActivity extends AppCompatActivity implements View.OnClickListener {
    private AppCompatImageButton ibBack;
    private ProgressDialog mProgressDialog;
    private WebView webView;

    public static void newInstance(Context context) {
        Intent intent = new Intent(context, NoticeDetailActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_notice_detail);
        ibBack = findViewById(R.id.ib_back);
        webView = findViewById(R.id.web_view);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        ibBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        boolean portrait = newConfig.orientation == Configuration.ORIENTATION_PORTRAIT;
        tryFullScreen(!portrait);
    }

    private void tryFullScreen(boolean fullScreen) {
        setFullScreen(fullScreen);
    }

    private void setFullScreen(boolean fullScreen) {
        WindowManager.LayoutParams attrs = getWindow().getAttributes();
        if (fullScreen) {
            attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().setAttributes(attrs);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else {
            attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(attrs);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }

    private void initView() {
        String mBean = "http://www.hg3-app.com/agre/";
        initWebView();
        webView.loadUrl(mBean);
    }

    private void initWebView() {
        // showProgressBarDialog();
        //初始化首页
        webView.setWebChromeClient(mWebChromeClient);
        webView.setWebViewClient(mWebViewClient);
    }


    @Override
    public void onClick(View v) {
        showProgressBarDialog();
    }

    private void showProgressBarDialog() {
        if (mProgressDialog == null)
            mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("我们正为您选择最快的路线,\n请耐心等待");
        mProgressDialog.setCanceledOnTouchOutside(true); // 点击加载框以外的区域
        mProgressDialog.show();
    }

    private WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            //do you work
            Log.i("Info", "progress:" + newProgress);
            if (newProgress == 100) {
                //mImageView.setVisibility(View.GONE);
                if (mProgressDialog != null)
                    mProgressDialog.dismiss();
            }

        }

    };
    private boolean isSuccess = false;
    private boolean isError = false;
    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            //do you  work
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (!isError) {
                isSuccess = true;
                //回调成功后的相关操作
                webView.setVisibility(View.VISIBLE);
            }
            isError = false;
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            //showErrorPage();//显示错误页面
            webView.setVisibility(View.GONE);
            isError = true;
            isSuccess = false;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
            super.onReceivedSslError(view, handler, error);
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 监听返回--是否退出程序
     */
    //改写物理按键——返回的逻辑，希望浏览的网页后退而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webView.canGoBack()) {
                webView.goBack();//返回上一页面
                return true;
            } else {
                //  System.exit(0);//退出程序
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    finish();
                    return true;
                }
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
